﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Rose.Models;


namespace Rose.Helper
{

    internal static partial class IEnumerableHelpModel
    {
        public static List<UserInfo> RemoveFriendAndFocus(this List<UserInfo> model, string selfId)
        {
            List<UserInfo> returnModel = new List<UserInfo>();
            foreach (var userInfo in model)
            {
                string userId = userInfo.Id;
                if (!(new IsExistHelpModel().IsFocusRelationExist(selfId, userId)) && !new IsExistHelpModel().IsFriendRelationExist(selfId, userId))
                    returnModel.Add(userInfo);
            }
            return returnModel;
        }



    }

    public class RecommendHelpModel
    {
        private   Database_RoseEntities db = new Database_RoseEntities();
      

        private  List<UserInfo> GetMajorFriends(string selfId,bool isOppGender,bool isDescending)
        {
            
            UserInfo selfInfo =new CheckHelpModel().CheckUserInfo(selfId);
            //检查安全
            bool selfGender = selfInfo.IsMan;
            if (isDescending)
            {
                if (!isOppGender)
                    return db.UserInfos.Where(c => c.University == selfInfo.University && c.College == selfInfo.College && c.Id != selfInfo.Id).OrderByDescending(c=>c.PhotoCount).ThenByDescending(c => c.BeFocusCount).ThenByDescending(c => c.Hit).ThenByDescending(c => c.FriendCount).ToList().RemoveFriendAndFocus(selfId);
                else
                    return db.UserInfos.Where(c => c.IsMan != selfGender && c.University == selfInfo.University && c.College == selfInfo.College && c.Id != selfInfo.Id).OrderByDescending(c=>c.PhotoCount).ThenByDescending(c => c.BeFocusCount).ThenByDescending(c => c.Hit).ThenByDescending(c => c.FriendCount).ToList().RemoveFriendAndFocus(selfId);
            }
            else
            {
                if (!isOppGender)
                    return db.UserInfos.Where(c => c.University == selfInfo.University && c.College == selfInfo.College && c.Id != selfInfo.Id).OrderBy(c => c.BeFocusCount).ThenBy(c => c.Hit).ThenBy(c => c.FriendCount).ToList().RemoveFriendAndFocus(selfId);
                else
                    return db.UserInfos.Where(c => c.IsMan != selfGender && c.University == selfInfo.University && c.College == selfInfo.College && c.Id != selfInfo.Id).OrderBy(c => c.BeFocusCount).ThenBy(c => c.Hit).ThenBy(c => c.FriendCount).ToList().RemoveFriendAndFocus(selfId);
            }
        }

        

        private  List<UserInfo> GetMajorFriends(string selfId, string   college, bool isOppGender,bool isDescending)
        {
            UserInfo selfInfo =new CheckHelpModel().CheckUserInfo(selfId);
            //检查安全
            bool selfGender = selfInfo.IsMan;
            if (isDescending)
            {
                if (!isOppGender)
                    return db.UserInfos.Where(c => c.University == selfInfo.University && c.College == college && c.Id != selfInfo.Id).OrderByDescending(c => c.PhotoCount).ThenByDescending(c => c.BeFocusCount).ThenByDescending(c => c.Hit).ThenByDescending(c => c.FriendCount).ToList().RemoveFriendAndFocus(selfId);
                else
                    return db.UserInfos.Where(c => c.IsMan != selfGender && c.University == selfInfo.University && c.College == college && c.Id != selfInfo.Id).OrderByDescending(c => c.PhotoCount).ThenByDescending(c => c.BeFocusCount).ThenByDescending(c => c.Hit).ThenByDescending(c => c.FriendCount).ToList().RemoveFriendAndFocus(selfId);
            }
            else
            {
                if (!isOppGender)
                    return db.UserInfos.Where(c => c.University == selfInfo.University && c.College == college && c.Id != selfInfo.Id).OrderBy(c => c.BeFocusCount).ThenBy(c => c.Hit).ThenBy(c => c.FriendCount).ToList().RemoveFriendAndFocus(selfId);
                else
                    return db.UserInfos.Where(c => c.IsMan != selfGender && c.University == selfInfo.University && c.College == college && c.Id != selfInfo.Id).OrderBy(c => c.BeFocusCount).ThenBy(c => c.Hit).ThenBy(c => c.FriendCount).ToList().RemoveFriendAndFocus(selfId);
            }
        }

        private  List<UserInfo> GetAddressFriends(string selfId, bool isOppGender,bool isDescending)
        {
            UserInfo selfInfo =new CheckHelpModel().CheckUserInfo(selfId);
            bool selfGender = selfInfo.IsMan;
            if (isDescending)
            {
                if (!isOppGender)
                    return db.UserInfos.Where(c => c.AddressCity== selfInfo.AddressCity&&c.AddressProvince==selfInfo.AddressProvince && c.Id != selfInfo.Id).OrderByDescending(c=>c.PhotoCount).ThenByDescending(c => c.BeFocusCount).ThenByDescending(c => c.Hit).ThenByDescending(c => c.FriendCount).ToList().RemoveFriendAndFocus(selfId);
                else
                    return db.UserInfos.Where(c => c.IsMan != selfGender && c.AddressCity == selfInfo.AddressCity && c.AddressProvince == selfInfo.AddressProvince && c.Id != selfInfo.Id).OrderByDescending(c=>c.PhotoCount).ThenByDescending(c => c.BeFocusCount).ThenByDescending(c => c.Hit).ThenByDescending(c => c.FriendCount).ToList().RemoveFriendAndFocus(selfId);
            }
            else
            {
                if (!isOppGender)
                    return db.UserInfos.Where(c => c.AddressCity == selfInfo.AddressCity && c.AddressProvince == selfInfo.AddressProvince && c.Id != selfInfo.Id).OrderBy(c => c.BeFocusCount).ThenBy(c => c.Hit).ThenBy(c => c.FriendCount).ToList().RemoveFriendAndFocus(selfId);
                else
                    return db.UserInfos.Where(c => c.IsMan != selfGender && c.AddressCity == selfInfo.AddressCity && c.AddressProvince == selfInfo.AddressProvince && c.Id != selfInfo.Id).OrderBy(c => c.BeFocusCount).ThenBy(c => c.Hit).ThenBy(c => c.FriendCount).ToList().RemoveFriendAndFocus(selfId);
            }
        }

        private  List<UserInfo> GetUniversityFriends(string selfId, bool isOppGender,bool isDescending)
        {
            UserInfo selfInfo =new CheckHelpModel().CheckUserInfo(selfId);
            bool selfGender = selfInfo.IsMan;
            if (isDescending)
            {
                if (!isOppGender)
                    return db.UserInfos.Where(c => c.University == selfInfo.University && c.Id != selfInfo.Id).OrderByDescending(c=>c.PhotoCount).ThenByDescending(c => c.BeFocusCount).ThenByDescending(c => c.Hit).ThenByDescending(c => c.FriendCount).ToList().RemoveFriendAndFocus(selfId);
                else
                    return db.UserInfos.Where(c => c.IsMan != selfGender && c.University == selfInfo.University && c.Id != selfInfo.Id).OrderByDescending(c=>c.PhotoCount).ThenByDescending(c => c.BeFocusCount).ThenByDescending(c => c.Hit).ThenByDescending(c => c.FriendCount).ToList().RemoveFriendAndFocus(selfId);
            }
            else
            {
                if (!isOppGender)
                    return db.UserInfos.Where(c => c.University == selfInfo.University && c.Id != selfInfo.Id).OrderBy(c => c.BeFocusCount).ThenBy(c => c.Hit).ThenBy(c => c.FriendCount).ToList().RemoveFriendAndFocus(selfId);
                else
                    return db.UserInfos.Where(c => c.IsMan != selfGender && c.University == selfInfo.University && c.Id != selfInfo.Id).OrderBy(c => c.BeFocusCount).ThenBy(c => c.Hit).ThenBy(c => c.FriendCount).ToList().RemoveFriendAndFocus(selfId);
            }

        }

        private  RecommendModel ChangeUserInfoToRecommendModel(UserInfo recommend)
        {
            return new RecommendModel()
            {
                UserId = recommend.Id,
                TrueName = recommend.Name ?? string.Empty,
                ImagePath = recommend.ImagePath ?? string.Empty,
                Address = recommend.Address ?? string.Empty,
                University = recommend.University ?? string.Empty,
                MajorClass = recommend.CollegeMajorClass ?? string.Empty,
                SchoolYear = recommend.SchoolYear.ToString(),
                Hobbies =new UserInfoHelpModel().GetHobbies(recommend.Id) ?? string.Empty,
                Gender = recommend.Gender,
                College=recommend.College
            };
        }

        private  List<RecommendModel> ChangeUserInfosToRecommendModels(IEnumerable<UserInfo> recommendList)
        {
            List<RecommendModel> returnModel = new List<RecommendModel>();
            foreach (UserInfo recommend in recommendList)
            {
                try
                {
                    returnModel.Add(ChangeUserInfoToRecommendModel(recommend));
                }
                catch
                {
                    continue;
                }
            }
            return returnModel;
        }

        public  List<RecommendModel> GetMajorRecommendFriend(string selfId, bool isOppGender,bool isDescending, int currentpage, int pagesize)
        {
            
            var recommendList = GetMajorFriends(selfId, isOppGender,isDescending).Skip((currentpage - 1) * pagesize).Take(pagesize);
            //if (recommendList.Count() <= 0)
            //    return null;
            return ChangeUserInfosToRecommendModels(recommendList);

             
           
        }

        public  List<RecommendModel> GetMajorRecommendFriend(string selfId, string   major, bool isOppGender,bool isDescending, int currentpage, int pagesize)
        {
           
            var recommendList = GetMajorFriends(selfId,major, isOppGender,isDescending).Skip((currentpage - 1) * pagesize).Take(pagesize);
            //if (recommendList.Count() <= 0)
            //    return null;

            return ChangeUserInfosToRecommendModels(recommendList);
        }

        public  List<RecommendModel> GetAddressRecommendFriend(string selfId, bool isOppGender,bool isDescending,int currentpage,int pagesize)
        {
            
            var recommendList = GetAddressFriends(selfId, isOppGender,isDescending).Skip((currentpage - 1) * pagesize).Take(pagesize);
            //if (recommendList.Count() <= 0)
            //    return null;


            return ChangeUserInfosToRecommendModels(recommendList);
        }
        public  List<RecommendModel> GetUniversityRecommendFriend(string selfId, bool isOppGender,bool isDescending, int currentpage, int pagesize)
        {
           
            var recommendList = GetUniversityFriends(selfId, isOppGender,isDescending).Skip((currentpage - 1) * pagesize).Take(pagesize);
            //if (recommendList.Count() <= 0)
            //    return null;

            return ChangeUserInfosToRecommendModels(recommendList);
        }

        public  int GetMajorFriendsCount(string selfId, bool isOppGender)
        {
            return GetMajorFriends(selfId, isOppGender,true).Count();
        }
        public  int GetMajorFriendsCount(string selfId, string  major, bool isOppGender)
        {
            return GetMajorFriends(selfId, major, isOppGender, true).Count();
        }
        public  int GetAddressFriendsCount(string selfId, bool isOppGender)
        {
            return GetAddressFriends(selfId, isOppGender, true).Count();
        }
        public  int GetUniversityFriendsCount(string selfId, bool isOppGender)
        {
            return GetUniversityFriends(selfId, isOppGender, true).Count();
        }
    }
}